package com.xy.biz.uc.service.impl;

import com.xy.biz.uc.service.UploadFileService;
import com.xy.lang.domain.Result;
import com.xy.tool.minio.domain.resp.AvatarInfoResponse;
import com.xy.tool.minio.domain.resp.FileUploadResponse;
import com.xy.tool.minio.util.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 于振晗
 * @title: UploadFileService
 * @description: TODO
 * @date 2021年02月23日
 * @path com.xy.biz.fd.service
 */
@Service
public class UploadFileServiceImpl implements UploadFileService {

    @Value("${user.head-portrait-path}")
    private String headPortraitPath;


    @Autowired
    private MinioUtil minioUtil;


    @Override
    public Result<?> uploadHeadPortrait(MultipartFile file) {
        try {
            FileUploadResponse fileUploadResponse = minioUtil.uploadFile(file, headPortraitPath);
            String netUrl = minioUtil.getObjectURL(headPortraitPath, fileUploadResponse.getFileName(), null);
            AvatarInfoResponse avatarInfoResponse = AvatarInfoResponse.builder().avatar(fileUploadResponse.getFileName()).avatarUrl(netUrl).build();
            return Result.success(avatarInfoResponse);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ofErrorRowsAffected();
    }
}
